import java.util.Stack;

public class Test {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String s : tokens) {
            if(!isOperator()){
                int x = Integer.parseInt(s);
                stack.push(x);
            }else {
                int val2 = stack.peek();
                stack.pop();
                int val1 = stack.peek();
                switch (s){
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 / val2);
                        break;
                }
            }
        }
        return stack.peek();
    }

    private boolean isOperator(String str){
        if(str == "+" ||str == "-" ||str == "*" ||str == "/") {
            return true;
        }
        return false;
    }
    //括号匹配问题
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i < s.length();i++){
            char ch = s.charAt(i);
            if(ch == '(' || ch == '[' || ch == '{'){
                stack.push(ch);
            } else{
                if(stack.isEmpty()){
                    return false;
                }
                char ch2 = stack.peek();
                if(ch2 == '(' && ch == ')' ||
                        ch2 == '[' && ch == ']' ||
                        ch2 == '{' && ch == '}' ){
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(!stack.isEmpty()){
            return false;
        }
        return true;
    }
    public static void main(String[] args) {

    }
}
